package com.company.oa.hr.controller.recruitment;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.common.api.Result;
import com.company.oa.hr.dto.recruitment.PositionDTO;
import com.company.oa.hr.entity.recruitment.Position;
import com.company.oa.hr.service.recruitment.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/hr/recruitment/position")
public class PositionController {

    @Autowired
    private PositionService positionService;

    /**
     * 分页查询职位列表
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('hr:recruitment:position:list')")
    public Result<IPage<Position>> getPositionPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            Position position) {
        IPage<Position> pageResult = positionService.getPositionPage(page, size, position);
        return Result.success(pageResult);
    }

    /**
     * 获取职位详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('hr:recruitment:position:info')")
    public Result<Position> getPositionDetail(@PathVariable Long id) {
        Position position = positionService.getById(id);
        return Result.success(position);
    }

    /**
     * 创建招聘职位
     */
    @PostMapping
    @PreAuthorize("hasAuthority('hr:recruitment:position:create')")
    public Result<Boolean> createPosition(@RequestBody @Valid PositionDTO positionDTO) {
        // 从当前登录用户中获取创建人信息
        Long creatorId = 1L; // 实际项目中应从SecurityContext中获取
        String creatorName = "管理员"; // 实际项目中应从SecurityContext中获取
        boolean success = positionService.createPosition(positionDTO, creatorId, creatorName);
        return Result.success(success);
    }

    /**
     * 更新招聘职位
     */
    @PutMapping
    @PreAuthorize("hasAuthority('hr:recruitment:position:update')")
    public Result<Boolean> updatePosition(@RequestBody @Valid PositionDTO positionDTO) {
        boolean success = positionService.updatePosition(positionDTO);
        return Result.success(success);
    }

    /**
     * 删除招聘职位
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('hr:recruitment:position:delete')")
    public Result<Boolean> deletePosition(@PathVariable Long id) {
        boolean success = positionService.deletePosition(id);
        return Result.success(success);
    }

    /**
     * 发布招聘职位
     */
    @PostMapping("/publish/{id}")
    @PreAuthorize("hasAuthority('hr:recruitment:position:publish')")
    public Result<Boolean> publishPosition(@PathVariable Long id) {
        boolean success = positionService.publishPosition(id);
        return Result.success(success);
    }

    /**
     * 暂停招聘职位
     */
    @PostMapping("/pause/{id}")
    @PreAuthorize("hasAuthority('hr:recruitment:position:pause')")
    public Result<Boolean> pausePosition(@PathVariable Long id) {
        boolean success = positionService.pausePosition(id);
        return Result.success(success);
    }

    /**
     * 结束招聘职位
     */
    @PostMapping("/end/{id}")
    @PreAuthorize("hasAuthority('hr:recruitment:position:end')")
    public Result<Boolean> endPosition(@PathVariable Long id) {
        boolean success = positionService.endPosition(id);
        return Result.success(success);
    }
}